/****************************************************************
 * <strong>MFUser</strong>
 *
 * <p>
 * MFUser
 * </p>
 *
 * @author AndyZhang 2011  cnet2001@163.com
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.login;

import com.thinkwide.data.core.MFString;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MFUser implements HttpSessionBindingListener {

    public static String key = "@usersOnline";
    private String id;
    private String loginId;
    private String name;
    private String password;
    private MFUserType userType;
    private String mobilePhone;
    private String telePhone;
    private String email;
    private Date loginTime;
    private String departmentName;
    private String departmentId;
    private String companyId;
    private String companyName;
    private String level;
    private String language;
    private Object data;
    private String ip;

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setUserType(MFUserType userType) {
        this.userType = userType;
    }

    public MFUserType getUserType() {
        return userType;
    }

    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;
    }

    public String getMobilePhone() {
        return mobilePhone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }

    public String getCompanyId() {
        return companyId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getLevel() {
        return level;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public String getLoginId() {
        return loginId;
    }

    public void valueBound(HttpSessionBindingEvent event) {
        HttpSession session = event.getSession();
        ServletContext ctx = session.getServletContext();
        Map<String, MFUser> map = (Map<String, MFUser>) ctx.getAttribute(key);
        if (map == null) {
            map = new HashMap<String, MFUser>();
            ctx.setAttribute(key, map);
        }

        for (Map.Entry<String, MFUser> item : map.entrySet()) {
            if (MFString.compareNoCase(item.getKey(), loginId)) return;
        }

        map.put(loginId, this);

    }

    public void valueUnbound(HttpSessionBindingEvent event) {
        HttpSession session = event.getSession();
        ServletContext ctx = session.getServletContext();
        Map<String, MFUser> map = (Map<String, MFUser>) ctx.getAttribute(key);

        map.remove(loginId);

    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getIp() {
        return ip;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getLanguage() {
        return language;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getData() {
        return data;
    }

    public void setTelePhone(String telePhone) {
        this.telePhone = telePhone;
    }

    public String getTelePhone() {
        return telePhone;
    }

}
